﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;

namespace DotNetNuke.Tests.UI.WatiN.BuildVerification
{
    [TestFixture]
    [DependsOn(typeof(InstallTests))]
    [DependsOn(typeof(UpgradeTests))]
    [DependsOn(typeof(LoginTests))]
    public class PortalsTests : BVTBase
    {
        #region Test Setup and TearDown

        public string siteType;

        [SetUp]
        public void SetUp()
        {
            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            if (!siteType.Equals("UpgradeOnly") && !siteType.Equals("InstallOnly"))
            {
                ieInstance.GoTo(SiteURL + "/logoff.aspx");
                //Remove the portal Folders
                Directory.Delete(PhysicalPath + @"\mytestportal", true);
                if (siteType.Contains("3"))
                {
                    Directory.Delete(PhysicalPath + @"\Portals\2", true);
                }
                else
                {
                    Directory.Delete(PhysicalPath + @"\Portals\1", true);
                }
            }            
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Add_New_Child_Portal(string version, string type)
        {
            siteType = type;
            if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\PortalsTests\AddNewChildPortal\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                try
                {
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                    //Assert.IsFalse(homePage.PageContainsErrors());
                    ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
                    if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnPortalsPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Portals Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(CapturePath + "PortalsExceptions.txt", SiteType.Equals("Qualifier"));

                        //Continue the test
                        ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
                    }
                    HostPortalPage portalPage = new HostPortalPage(homePage);

                    //Act
                    portalPage.AddNewPortalLink.Click();
                    System.Threading.Thread.Sleep(1500);
                    portalPage.ChildRadioButton.Click();
                    System.Threading.Thread.Sleep(3000);
                    portalPage.PortalAliasField.Value = ("localhost/" + ApplicationName + "/MyTestPortal");
                    portalPage.PortalTitle.Value = "My Test Portal";
                    portalPage.PortalTemplateSelect.Select("Default Website");
                    portalPage.PortalTemplateSelect.Refresh();
                    System.Threading.Thread.Sleep(1000);
                    portalPage.AdminUserNameField.Value = TestUsers.Admin2.UserName;
                    portalPage.AdminFirstNameField.Value = TestUsers.Admin2.FirstName;
                    portalPage.AdminLastNameField.Value = TestUsers.Admin2.LastName;
                    portalPage.AdminEmailField.Value = TestUsers.Admin2.Email;
                    portalPage.AdminPasswordField.Value = TestUsers.Admin2.Password;
                    portalPage.AdminConfirmField.Value = TestUsers.Admin2.Password;
                    portalPage.CreatePortalLink.Click();
                    System.Threading.Thread.Sleep(3500);
                    portalPage.MessageLink.Click();

                    //Assert 
                    if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnPortalsPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Portals Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(CapturePath + "PortalsExceptions.txt", SiteType.Equals("Qualifier"));
                    }
                    ieInstance.GoTo(SiteURL + "/mytestportal/Host/Portals.aspx");
                    if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnPortalsPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Portals Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(CapturePath + "PortalsExceptions.txt", SiteType.Equals("Qualifier"));

                        //Continue the test
                        ieInstance.GoTo(SiteURL + "/mytestportal/Host/Portals.aspx");
                    }
                    TableCell portalNumber = portalPage.GetPortalNumberCellByPortalName("My Test Portal");
                    TableCell portalTitle = portalPage.GetPortalTitleCellByPortalName("My Test Portal");
                    TableCell portalAlias = portalPage.GetPortalAliasCellByPortalName("My Test Portal");
                    if (type.Contains("3"))
                    {
                        watinAssert.AssertIsTrue(portalNumber.InnerHtml.Contains("2"), "PortalTable.jpg");
                    }
                    else
                    {
                        watinAssert.AssertIsTrue(portalNumber.InnerHtml.Contains("1"), "PortalTable.jpg");
                    }
                    watinAssert.AssertIsTrue(portalTitle.InnerHtml.Contains("My Test Portal"), "PortalTable2.jpg");
                }
                catch (Exception e)
                {
                    if (!e.ToString().Contains("AssertionFailureException"))
                    {
                        ieInstance.CaptureWebPageToFile(capturePath + "Exception.jpg");
                        Assert.IsTrue(false, "An Exception Occured during the test, view the screen shot for more information. " + System.Environment.NewLine + e.ToString());
                    }
                    else
                    {
                        throw e;
                    }
                }
            }

        }

        #endregion
    }
}

